/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package org.apache.hadoop.fs.obs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.obs.security.AccessType;
import org.apache.hadoop.fs.obs.security.AuthorizeProvider;
import org.apache.hadoop.fs.obs.security.DelegationTokenCapability;
import org.apache.hadoop.fs.obs.security.RangerAuthorizeProvider;
import org.apache.hadoop.security.token.Token;
import org.apache.ranger.obs.client.ClientConstants;
import org.apache.ranger.obs.server.DistributedMiniServerBaseTest;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;

/**
 * 功能描述
 *
 * @since 2021-09-22
 */
public class RangerAuthorizeProviderTest extends DistributedMiniServerBaseTest {

    private static AuthorizeProvider authorizer;

    @BeforeClass
    public static void setUp() throws Exception {
        String principal = "ranger/hadoop@EXAMPLE.COM";
        authorizer = new RangerAuthorizeProvider();
        Configuration client_conf = new Configuration();
        client_conf.set("ranger.obs.service.rpc.address", "127.0.0.1:26901;127.0.0.1:26902");
        client_conf.set("ranger.obs.service.kerberos.principal", principal);
        authorizer.init(client_conf);
    }

    @Test
    public void testIsAuthorized() throws IOException {
        boolean authorized = authorizer.isAuthorized("test", "test", AccessType.WRITE);
        Assert.assertFalse("testIsAuthorized", authorized);
    }

    @Test
    public void testGetDelegationToken() throws IOException {
        if (authorizer instanceof DelegationTokenCapability) {
            Token<?> yarn = ((DelegationTokenCapability) authorizer).getDelegationToken("yarn");
            Assert.assertNotNull("DelegationToken", yarn);
        }
    }

    @Test
    public void testGetCanonicalServiceName() {
        if (authorizer instanceof DelegationTokenCapability) {
            String canonicalServiceName = ((DelegationTokenCapability) authorizer).getCanonicalServiceName();
            Assert.assertEquals("testGetCanonicalServiceName",
                ClientConstants.DEFAULT_RANGER_OBS_SERVICE_DT_SERVICE_NAME, canonicalServiceName);
        }
    }
}